﻿using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CloudStorageLight.Core
{
    public class UserInfo : TableEntity
    {

        public UserInfo()
        {

        }

        public UserInfo(string id)
            :base("", id)
        {
        }

        public string Name { get { return this.RowKey; } }

        public string EncryptedFileProtectedPassword { get; set; }

        private static string GetTableName(string accountName)
        {
            return accountName + "UserInfo";
        }

        public void Save(string connectionString, string accountName)
        {
            var tableName = GetTableName(accountName);
            var storageAccount = CloudStorageAccount.Parse(connectionString);
            CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
            TableBatchOperation batchOperation = new TableBatchOperation();
            var table = tableClient.GetTableReference(tableName);
            table.CreateIfNotExists();
            batchOperation.InsertOrReplace(this);
            table.ExecuteBatch(batchOperation);
        }

        public static IEnumerable<UserInfo> List(string connectionString, string accountName)
        {
            var tableName = GetTableName(accountName);
            var storageAccount = CloudStorageAccount.Parse(connectionString);
            CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
            var table = tableClient.GetTableReference(tableName);
            if (!table.Exists()) return null;

            var query = new TableQuery<UserInfo>();
            query = query.Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, ""));
            var list = table.ExecuteQuery(query);

            return list;

        }

        public static UserInfo Get(string connectionString, string accountName, string id)
        {
            var tableName = GetTableName(accountName);
            var storageAccount = CloudStorageAccount.Parse(connectionString);
            CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
            var table = tableClient.GetTableReference(tableName);
            if (!table.Exists()) return null;

            var query = new TableQuery<UserInfo>();
            query = query.Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, ""));
            query = query.Where(TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, id));
            var list = table.ExecuteQuery(query);
            return list.FirstOrDefault();

        }

        public static void Delete(string connectionString, string accountName, string id)
        {
            var tableName = GetTableName(accountName);
            var storageAccount = CloudStorageAccount.Parse(connectionString);
            CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
            TableBatchOperation batchOperation = new TableBatchOperation();
            var table = tableClient.GetTableReference(tableName);
            if (!table.Exists()) return;

            var query = new TableQuery<UserInfo>();
            query = query.Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, ""));
            query = query.Where(TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, id));
            var list = table.ExecuteQuery(query);
            foreach (var e in list)
            {
                batchOperation.Delete(e);

            }
            table.ExecuteBatch(batchOperation);

        }
    }
}
